<!-- begin of sidebar -->
<div th:fragment="jcms-sidebar" class="jcms-sidebar" data-options="region:'west',split:true,border:true,title:'导航菜单'">
	<div class="easyui-accordion" data-options="border:false,fit:true">
		<div th:each="bar,index :${bartree}" th:title="${bar.name}" data-options="iconCls:'icon-application-form-edit'" style="padding: 5px;">
			<ul th:id="${bar.id}" class="easyui-tree jcms-side-tree"></ul>
			<script type="text/javascript" th:inline="javascript">
				/*<![CDATA[*/$(function() {jcmsSidebarTab.init($('#[(${bar.id})]'), [(${index.index})]);});/*]]>*/
			</script>
		</div>
	</div>
	<script type="text/javascript" th:inline="javascript">
		/*<![CDATA[*/
		(function(window, $) {
			$(function() {
				/**
				 * 选项卡初始化
				 */
				$('#jcms-tabs').tabs({
					tools : [ {
						iconCls : 'icon-reload',
						border : false,
						handler : function() {
							var tab = $('#jcms-tabs').tabs('getSelected');
							var id = tab.panel('options').id;
							if (id && jcmsSidebarTab.__tabs && jcmsSidebarTab.__tabs.length > 0) {
								for ( var i in jcmsSidebarTab.__tabs) {
									var item = jcmsSidebarTab.__tabs[i];
									if (item.id === id) {
										$('#jcms-tabs').tabs('update', {
											tab : tab,
											options : item
										});
									}
								}
							}
						}
					} ],
					onSelect : function(title, index) {
						var tab = $('#jcms-tabs').tabs('getTab', index);
						if (jcmsSidebarTab.__tabs && jcmsSidebarTab.__tabs.length > 0) {
							for ( var i in jcmsSidebarTab.__tabs) {
								var item = jcmsSidebarTab.__tabs[i];
								if (item.id === tab.panel('options').id) {
									var tab = $('#jcms-tabs').tabs('getTab', index);
									$('#jcms-tabs').tabs('update', {
										tab : tab,
										options : item
									});
								}
							}
						}
					}
				});
			});

			var sidebar = [(${sidebar})];
			window.jcmsSidebarTab = {
				__tabs : [],
				sidebar:sidebar,
				init(doc,i){
					$(function() {
						doc.tree({
							data : jcmsSidebarTab.sidebar[i].bar,
							onClick : function(node) {
								jcmsSidebarTab.addTab(node.text, jcmsSidebarTab.getIframeLink(node.attributes), node.iconCls, node.id);
							}
						});
					});
				},
				getIframeLink: function(attributes){
					var link = typeof attributes === 'undefined' || typeof attributes['link'] === 'undefined' ? '' : attributes['link'];
					return link;
				},
				/**
				 * addTab 添加菜单选项
				 * Param title 名称
				 * Param href 链接
				 * Param iconCls 图标样式
				 * Param iframe 链接跳转方式（true为iframe，false为href）
				 */
				addTab : function(title, href, iconCls, id) {
					if (!href || href.length < 1) {
						return;
					}
					// console.log(id);
					var tabPanel = $('#jcms-tabs');
					var content = '<iframe class="iframe-tag" scrolling="auto" frameborder="0"  src="' + href + '" style="width:100%;height:100%;"></iframe>';

					var tab = null;
					var index = null;
					if (tabPanel.tabs('tabs').length > 0) {
						var tab = null;
						for (var i = 0; i < tabPanel.tabs('tabs').length; i++) {
							var inId = tabPanel.tabs('getTab', i).panel('options').id;
							if (inId === id) {
								tab = tabPanel.tabs('getTab', i);
								index = i;
								break;
							}
						}
					}
					var tabParam = {
						id : id,
						title : title,
						content : content,
						iconCls : iconCls,
						bodyCls : 'noyscolle',
						fit : true,
						closable : true
					};
					if (tab) {
						// 更新
						$('#jcms-tabs').tabs('select', index);
						$('#jcms-tabs').tabs('update', {
							tab : tab,
							options : tabParam
						});
					} else {
						// 新增
						tabPanel.tabs('add', tabParam);
						this.__tabs.push(tabParam);
					}
				}
			}
		})(window, $);
		/*]]>*/
	</script>
</div>
<!-- end of sidebar -->
